package com.chuangke.lowcode.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.chuangke.common.db.query.dto.QueryDto;
import com.chuangke.common.http.HttpResult;
import com.chuangke.framework.model.BillModel;
import com.chuangke.lowcode.EntityModelConstants;
import com.chuangke.lowcode.LowCodeContext;
import com.chuangke.lowcode.LowCodeExecutor;
import com.chuangke.lowcode.model.EntityModel;
import com.chuangke.metadata.entity.SysBill;
import com.chuangke.metadata.service.SysBillService;

import cn.hutool.core.util.StrUtil;

@RestController
public class LowCodeController {
	
	@Autowired
	private LowCodeExecutor lowCodeExecutor;
	
	@Autowired
	private SysBillService sysBillService ;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/bill/save/{formId}")
	public HttpResult save(@PathVariable("formId") String formId,@RequestBody List<JSONObject> datas) {
		if(CollectionUtils.isEmpty(datas)){
			return HttpResult.ok();
		}

		SysBill sysBill = sysBillService.findById(formId) ;
		
		EntityModel entityModel = EntityModelConstants.getEntityModel(sysBill.getBusinessEntity());

		List ndatas = datas.stream().map(data -> data.to(entityModel.getEntityClass())).collect(Collectors.toList()) ;

		LowCodeContext context = new LowCodeContext().setDatas(ndatas).setFormId(formId)
				.setEntityModel(entityModel);

		BillModel bill = (BillModel) ndatas.get(0);
		if(StrUtil.isEmpty(bill.getId())){
			lowCodeExecutor.saveBill(context);
		}else{
			lowCodeExecutor.updateBill(context);
		}
		
		List<String> billIds = (List<String>) ndatas.stream().map(data -> ((BillModel)data).getBillId()).collect(Collectors.toList()) ;

		return load(formId,billIds);
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "bill/delete/{formId}")
	public HttpResult delete(@PathVariable("formId") String formId,@RequestBody List<String> billIds) {
		SysBill sysBill = sysBillService.findById(formId) ;
		LowCodeContext context = new LowCodeContext().setDatas(billIds)
				.setEntityModel(EntityModelConstants.getEntityModel(sysBill.getBusinessEntity()));
		lowCodeExecutor.deleteBill(context);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "bill/load/{formId}")
	public HttpResult load(@PathVariable("formId") String formId,@RequestBody List<String> billIds) {
		SysBill sysBill = sysBillService.findById(formId) ;
		LowCodeContext context = new LowCodeContext().setDatas(billIds)
				.setEntityModel(EntityModelConstants.getEntityModel(sysBill.getBusinessEntity()));

		lowCodeExecutor.loadBill(context);

		return HttpResult.ok(context.getResult());
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "bill/page/{formId}")
	public HttpResult page(@PathVariable("formId") String formId,@RequestBody QueryDto queryDto) {
		SysBill sysBill = sysBillService.findById(formId) ;
		LowCodeContext context = new LowCodeContext()
				.setEntityModel(EntityModelConstants.getEntityModel(sysBill.getBusinessEntity())).
				setConditions(queryDto.getConditions()).setOrders(queryDto.getOrders()).setPage(queryDto.getPage());

		lowCodeExecutor.page(context) ;

		return HttpResult.ok(context.getResult());
	}

}
